<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="入住类型(普通/钟点)" prop="checkType">
        <el-select v-model="queryParams.checkType" placeholder="请选择入住类型(普通/钟点)" clearable>
          <el-option
            v-for="dict in dict.type.hot_way_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="客户名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入客户名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系电话" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入联系电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="身份证号" prop="idCard">
        <el-input
          v-model="queryParams.idCard"
          placeholder="请输入身份证号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="入住金额" prop="pay">
        <el-input
          v-model="queryParams.pay"
          placeholder="请输入入住金额"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="实际入住时间" prop="checkInTime">
        <el-date-picker clearable
          v-model="queryParams.checkInTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择实际入住时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="预计入住时长" prop="duration">
        <el-input
          v-model="queryParams.duration"
          placeholder="请输入预计入住时长"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="押金金额" prop="deposit">
        <el-input
          v-model="queryParams.deposit"
          placeholder="请输入押金金额"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否续住(Y续住,N新入住)" prop="isRenew">
        <el-input
          v-model="queryParams.isRenew"
          placeholder="请输入是否续住(Y续住,N新入住)"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否退房" prop="isCheckout">
        <el-select v-model="queryParams.isCheckout" placeholder="请选择是否退房" clearable>
          <el-option
            v-for="dict in dict.type.sys_yes_no"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="实际退房时间" prop="checkoutTime">
        <el-date-picker clearable
          v-model="queryParams.checkoutTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择实际退房时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="额外费用" prop="extraPay">
        <el-input
          v-model="queryParams.extraPay"
          placeholder="请输入额外费用"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['hotal:checkin:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['hotal:checkin:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['hotal:checkin:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['hotal:checkin:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="checkinList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="客户入住id" align="center" prop="checkinId" />
      <el-table-column label="关联预订id" align="center" prop="reservationId" />
      <el-table-column label="房间id" align="center" prop="roomId" />
      <el-table-column label="入住类型(普通/钟点)" align="center" prop="checkType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.hot_way_type" :value="scope.row.checkType"/>
        </template>
      </el-table-column>
      <el-table-column label="入住客户id" align="center" prop="guestId" />
      <el-table-column label="客户名" align="center" prop="name" />
      <el-table-column label="联系电话" align="center" prop="phone" />
      <el-table-column label="身份证号" align="center" prop="idCard" />
      <el-table-column label="入住金额" align="center" prop="pay" />
      <el-table-column label="实际入住时间" align="center" prop="checkInTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.checkInTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="预计入住时长" align="center" prop="duration" />
      <el-table-column label="押金金额" align="center" prop="deposit" />
      <el-table-column label="是否续住(Y续住,N新入住)" align="center" prop="isRenew">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.isRenew"/>
        </template>
      </el-table-column>
      <el-table-column label="关联新入住id(仅是否续住为Y有)" align="center" prop="relatedCheckinId" />
      <el-table-column label="入住/续住操作人id" align="center" prop="checkinUserId" />
      <el-table-column label="是否退房" align="center" prop="isCheckout">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.isCheckout"/>
        </template>
      </el-table-column>
      <el-table-column label="实际退房时间" align="center" prop="checkoutTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.checkoutTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="额外费用" align="center" prop="extraPay" />
      <el-table-column label="退房操作人" align="center" prop="checkoutUserId" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['hotal:checkin:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['hotal:checkin:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改客户入住对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="入住类型(普通/钟点)" prop="checkType">
          <el-select v-model="form.checkType" placeholder="请选择入住类型(普通/钟点)">
            <el-option
              v-for="dict in dict.type.hot_way_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="客户名" prop="name">
          <el-input v-model="form.name" placeholder="请输入客户名" />
        </el-form-item>
        <el-form-item label="联系电话" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入联系电话" />
        </el-form-item>
        <el-form-item label="身份证号" prop="idCard">
          <el-input v-model="form.idCard" placeholder="请输入身份证号" />
        </el-form-item>
        <el-form-item label="入住金额" prop="pay">
          <el-input v-model="form.pay" placeholder="请输入入住金额" />
        </el-form-item>
        <el-form-item label="实际入住时间" prop="checkInTime">
          <el-date-picker clearable
            v-model="form.checkInTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择实际入住时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="预计入住时长" prop="duration">
          <el-input v-model="form.duration" placeholder="请输入预计入住时长" />
        </el-form-item>
        <el-form-item label="押金金额" prop="deposit">
          <el-input v-model="form.deposit" placeholder="请输入押金金额" />
        </el-form-item>
        <el-form-item label="是否续住(Y续住,N新入住)" prop="isRenew">
          <el-input v-model="form.isRenew" placeholder="请输入是否续住(Y续住,N新入住)" />
        </el-form-item>
        <el-form-item label="是否退房" prop="isCheckout">
          <el-select v-model="form.isCheckout" placeholder="请选择是否退房">
            <el-option
              v-for="dict in dict.type.sys_yes_no"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="实际退房时间" prop="checkoutTime">
          <el-date-picker clearable
            v-model="form.checkoutTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择实际退房时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="额外费用" prop="extraPay">
          <el-input v-model="form.extraPay" placeholder="请输入额外费用" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="删除标志" prop="delFlag">
          <el-input v-model="form.delFlag" placeholder="请输入删除标志" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCheckin, getCheckin, delCheckin, addCheckin, updateCheckin } from "@/api/hotal/checkin";

export default {
  name: "Checkin",
  dicts: ['sys_yes_no', 'hot_way_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 客户入住表格数据
      checkinList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        reservationId: null,
        roomId: null,
        checkType: null,
        guestId: null,
        name: null,
        phone: null,
        idCard: null,
        pay: null,
        checkInTime: null,
        duration: null,
        deposit: null,
        isRenew: null,
        relatedCheckinId: null,
        checkinUserId: null,
        isCheckout: null,
        checkoutTime: null,
        extraPay: null,
        checkoutUserId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询客户入住列表 */
    getList() {
      this.loading = true;
      listCheckin(this.queryParams).then(response => {
        this.checkinList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        checkinId: null,
        reservationId: null,
        roomId: null,
        checkType: null,
        guestId: null,
        name: null,
        phone: null,
        idCard: null,
        pay: null,
        checkInTime: null,
        duration: null,
        deposit: null,
        isRenew: null,
        relatedCheckinId: null,
        checkinUserId: null,
        isCheckout: null,
        checkoutTime: null,
        extraPay: null,
        checkoutUserId: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        delFlag: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.checkinId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加客户入住";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const checkinId = row.checkinId || this.ids
      getCheckin(checkinId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改客户入住";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.checkinId != null) {
            updateCheckin(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCheckin(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const checkinIds = row.checkinId || this.ids;
      this.$modal.confirm('是否确认删除客户入住编号为"' + checkinIds + '"的数据项？').then(function() {
        return delCheckin(checkinIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('hotal/checkin/export', {
        ...this.queryParams
      }, `checkin_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
